define(
    function (require) {
        require('tpl!./list.tpl.html');

        var util = require('common/util');

        var globalData = require('common/GlobalData').getInstance();
        var system = globalData.getSystem();
        var user = globalData.getUser();
        var categoryList = system.experienceMallCategoryList;
        if (user.role === 'SHOP') {
            categoryList = system.experienceShopCategoryList;
        }

        var fields = [
            {
                title: '编号',
                field: 'id' ,
                sortable: true,
                resizable: true,
                width: 20,
                content: function (item) {
                    return item.id;
                }
            },
            {
                title: '活动主题',
                field: 'title' ,
                resizable : true,
                width: 100,
                content: function (item) {
                    return '<span class="list-link" data-command-args="' + item.id + '" data-command="modify">' + item.title + '</a>';
                }
            },
            {
                title: '活动分类',
                field: 'category' ,
                sortable: true,
                resizable : true,
                width: 70,
                content: function (item) {
                    var category = util.find(categoryList, function (category) {return category.value == item.category})
                    if (!util.isEmpty(category)) {
                        return category.text;
                    }
                    return '未知';
                }
            },
            {
                title: '开始时间',
                field: 'startTime' ,
                sortable: true,
                resizable : true,
                width: 100,
                content: function (item) {
                    return item.startTime;
                }
            },
            {
                title: '结束时间',
                field: 'endTime' ,
                sortable: true,
                resizable : true,
                width: 100,
                content: function (item) {
                    return item.endTime;
                }
            },
            {
                title: '活动性质',
                field: 'type' ,
                width: 100,
                content: function (item) {
                    var text = [undefined, "免费", "收费", "报名"][item.type];
                    // 收费活动添加票务链接
                    if (item.type === '2') {
                        text += ' <span class="list-link" data-command-args="'
                            + item.id
                            + '" data-command="ticket">票务信息</a></span>';
                    }
                    return text;
                }
            },
            {
                title: '操作',
                field: 'operate' ,
                width: 60,
                content: function (item) {
                    var operates = [
                        '<span class="list-link" data-command-args="'
                            + item.id
                            + '" data-command="modify">修改</a></span>',
                        '<span class="list-link" data-command-args="'
                            + item.id
                            + '" data-command="delete">删除</a></span>'
                    ];
                    return operates.join('');
                }
            }
        ];

        var BaseListView = require('common/ListView');

        function ListView() {
            BaseListView.apply(this, arguments);
        }

        ListView.prototype.template = 'experienceListPage';

        ListView.prototype.getUIProperties = function () {
            BaseListView.prototype.getUIProperties.apply(this, arguments);
            return {
                list: {
                    fields: fields,
                    sortable: false,
                    columnResizable: true,
                    subrow: false,
                    followHead: true,
                    selectMode: 'line',
                    noDataHtml: '<p style="margin:0;">暂无活动!</p>'
                }
            };
        }


        ListView.prototype.enterDocument = function () {
            BaseListView.prototype.enterDocument.apply(this, arguments);

            var me = this;
            this.get('list').on(
                'command',
                function (event) {
                    me.fire(event.name, event);
                }
            );

            this.get('createButton').on(
                'click',
                this.fire.bind(this, 'create')
            );

            this.get('list').initChildren();
        }

        require('er/util').inherits(ListView, BaseListView);
        return ListView;
    }
);
